package com.example.com.skills_utf8;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.app.umeng.Umeng;
import com.app.util.Utils;
import com.umeng.message.UTrack;
import com.umeng.message.UmengBaseIntentService;
import com.umeng.message.UmengIntentService;
import com.umeng.message.entity.UMessage;
import com.utils.g;

import org.android.agoo.client.BaseConstants;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by matengfei on 15/7/21.
 */
public class _62PushIntentService extends UmengBaseIntentService {

    private static String TAG = _62PushIntentService.class.getSimpleName();


    public interface IMessage
    {
        public void OnMessage(UMessage data) throws JSONException;
    }

    /*
    private static List<IMessage> message_interface_array = new ArrayList<IMessage>();
    public static void setMessageInterface( IMessage message){

        message_interface_array.add(message);

    }
    */
    private static IMessage mMessage ;
    public static void setMessageInterface( IMessage message ){
        mMessage = message;
    }

    private static Handler handler;
    public static void setHandler(Handler h){
        handler = h;
    }


    @Override
    protected void onMessage(Context context, Intent intent) {
        super.onMessage(context, intent);
        try {
            String message = intent.getStringExtra(BaseConstants.MESSAGE_BODY);
            UMessage msg = new UMessage(new JSONObject(message));
            UTrack.getInstance(context).trackMsgClick(msg);
            // code  to handle message here
            // ...
            /*
            for(int i =0; i<message_interface_array.size(); i++){
                IMessage im = message_interface_array.get(i);
                if(im != null)
                    im.OnMessage(msg);
            }
            */

            Log.e(TAG, "----------------------------------");

            /* 函数得不到执行...
            // 同一时刻只允许一个页面接受消息
            if(mMessage != null)
                mMessage.OnMessage(msg);
            */

            /*
            if(handler != null)
            {
                Message m = new Message();
                m.obj = msg;
                boolean bSend = handler.sendMessage(m);
                Log.e(TAG, "bSend: "+bSend);
            }
            */

            /*
            Intent intent_send = new Intent();
            intent_send.putExtra("data", msg.getRaw().toString());
            intent_send.setAction(g.Intent.ACTION_MSG_COME);
            sendBroadcast(intent_send);
            */

            {
                Intent intent1 = new Intent(context, _62_1Activity.class);
                intent1.putExtra("data", msg.getRaw().toString());
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent1);

            }



            Log.e(TAG, msg.getRaw().toString());

        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }


    }


}
